<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      echartInstance: null,
      allData: null, //数据
      currentPage: 1, //展示的页数
      totalPage: 0, //一共有多少页
      timeId: null
    };
  },
  created() {
    this.$socket.registerCallBack("sellerData", this.getData);
  },
  destroyed() {
    clearInterval(this.timeId);
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("sellerData");
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "sellerData",
      chartName: "seller",
      value: ""
    });
    window.addEventListener("resize", this.screenAdapter);
    // 在刚进入页面时，主动进行屏幕适配
    this.screenAdapter();
  },
  methods: {
    // 初始化echartInstance对象
    initChart() {
      this.echartInstance = this.$echarts.init(
        this.$refs.seller_ref,
        this.theme
      );
      this.echartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.echartInstance.on("mouseout", () => {
        this.startInterval();
      });

      const initOption = {
        // 设置标题
        title: {
          text: "▎商家销售量统计",

          // 设置标题位置
          left: 20,
          top: 20
        },
        // 设置图表位置
        grid: {
          top: "15%",
          left: "3%",
          bottom: "3%",
          right: "6%",
          containLabel: true //包含坐标轴上的文字
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            z: -1,
            type: "line",
            lineStyle: {
              color: "#2d3444"
            }
          }
        },
        xAxis: {
          type: "value"
        },
        yAxis: {
          type: "category"
        },
        series: [
          {
            type: "bar",
            // 说明图形的一些数据信息，比如值，名称等
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white"
              }
            },

            // 设置每个柱子的样式
            itemStyle: {
              // 柱子颜色渐变
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "#83bff6" },
                { offset: 0.5, color: "#188df0" },
                { offset: 1, color: "#188df0" }
              ])
            }
          }
        ]
      };
      this.echartInstance.setOption(initOption);
    },

    // 获取服务器数据
    getData(ret) {
      // const { data: ret } = await this.$http.get("seller");
      console.log(ret);
      this.allData = ret;

      // 对数据排序
      this.allData.sort((a, b) => {
        return a.value - b.value; //从小到大排序
      });
      // 每5个数据一页
      this.totalPage =
        this.allData.length % 5 == 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;
      console.log(this.totalPage);
      this.updateChart();
      // 启动定时器
      this.startInterval();
    },
    // 更新图表
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const sellerNames = showData.map(item => {
        return item.name;
      });
      const sellerValues = showData.map(item => {
        return item.value;
      });

      // 数据相关配置项
      const dataOption = {
        yAxis: {
          data: sellerNames
        },
        series: [
          {
            data: sellerValues
          }
        ]
      };
      this.echartInstance.setOption(dataOption);
    },
    // 设置定时器
    startInterval() {
      if (this.timeId) clearInterval(this.timeId);
      this.timeId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) this.currentPage = 1;
        this.updateChart();
      }, 3000);
    },
    // 当屏幕大小发生改变时，完成屏幕适配
    screenAdapter() {
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;

      // 分辨率相关配置相关
      const adapterOption = {
        // 设置标题
        title: {
          // 设置标题样式
          textStyle: {
            fontSize: titleFontSize
          }
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize //提示框宽度
            }
          }
        },

        series: [
          {
            // 设置柱子宽度
            barWidth: titleFontSize,

            // 设置每个柱子的样式
            itemStyle: {
              // 柱子圆角
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
            }
          }
        ]
      };
      this.echartInstance.setOption(adapterOption);
      this.echartInstance.resize();
    }
  },
  computed: {
    ...mapState(["theme"])
  },
  watch: {
    theme() {
      // 销毁当前图表
      this.echartInstance.dispose();
      // 重新创建图表
      this.initChart();
      // 完成屏幕适配
      this.screenAdapter();
      // 更新图表的展示
      this.updateChart();
    }
  }
};
</script>
<style scoped lang='less'>
</style>